﻿using CKS.Core.Framework;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Microsoft.IdentityModel.Tokens;
using System.Linq;
using ZR.Model;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Repository;
using ZR.Service.IService.ProductionControl;

namespace ZR.Service.Service.ProductionControl {
    /// <summary>
    /// 批次追溯Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksBatchCodeDataService), ServiceLifetime = LifeTime.Transient)]
    public class CksBatchCodeDataService : BaseService<CksBatchCodeData>, ICksBatchCodeDataService {

        /// <summary>
        /// 查询批次追溯列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksBatchCodeDataDto> GetList(CksBatchCodeDataQueryDto parm) {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .LeftJoin<PlcConfig>((a, b) => a.PlcId == b.Id)
                .WhereIF(!string.IsNullOrEmpty(parm.ProductionLineCode), (a, b) => b.ProductionLineCode == parm.ProductionLineCode)
                .WhereIF(!string.IsNullOrEmpty(parm.WorkstationCode), (a, b) => b.WorkstationCode == parm.WorkstationCode)
                .Where(predicate.ToExpression())
                .Select((a, b) => new CksBatchCodeDataDto {
                    Id = a.Id,
                    PlcId = a.PlcId,
                    BatchCode = a.BatchCode,
                    DbBlock = a.DbBlock,
                    Type = a.Type.ToString(),
                    Nullity = a.Nullity,
                    CreateTime = a.CreateTime,
                    ModifyTime = a.ModifyTime,
                    IsDeleted = a.IsDeleted,
                    ProductionLineCode = b.ProductionLineCode,
                    WorkstationCode = b.WorkstationCode
                }).ToPage(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksBatchCodeData GetInfo(int Id) {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加批次追溯
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksBatchCodeData AddCksBatchCodeData(CksBatchCodeData model) {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改批次追溯
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksBatchCodeData(CksBatchCodeData model) {
            return Update(model, true);
        }

        /// <summary>
        /// 清空批次追溯
        /// </summary>
        /// <returns></returns>
        public bool TruncateCksBatchCodeData() {
            var newTableName = $"cks_batch_code_data_{DateTime.Now:yyyyMMdd}";
            if (Queryable().Any() && !Context.DbMaintenance.IsAnyTable(newTableName)) {
                Context.DbMaintenance.BackupTable("cks_batch_code_data", newTableName);
            }

            return Truncate();
        }


        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksBatchCodeData> QueryExp(CksBatchCodeDataQueryDto parm) {
            var predicate = Expressionable.Create<CksBatchCodeData>();
            predicate.AndIF(!string.IsNullOrEmpty(parm.BatchCode), a => a.BatchCode.Contains(parm.BatchCode));
            return predicate;
        }
    }
}